/* Copyright (C) 2008, 2009, 2010 Faindu GmbH, Switzerland.
* All Rights Reserved. The following is Source Code and is subject to all restrictions 
* on such code as contained in the license accompanying this project. */
package blocstac.editimage.crop
{
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.events.MouseEvent;
	
/**
 * The user drags these "buttons" to edit the image.
 */
public class CropControl extends Sprite
{

	function CropControl(position:int, resizeCropMask:Function) {
		_position = position
		_resizeCropMask = resizeCropMask
	}

	private static const LINE_THICKNESS: Number = 1
	private static const BACKGROUND_COLOR: uint = 0x222222
	private static const LINE_COLOR: uint = 0x888888
	private static const CONTROL_SIZE:Number = 16 
	public static const CONTROL_HALF_SIZE:Number = CONTROL_SIZE/2
	
	public static const LEFT:int = 0
	public static const TOP:int = 1
	public static const RIGHT:int = 2
	public static const BOTTOM:int = 3
	public static const TOP_LEFT:int = 4
	public static const TOP_RIGHT:int = 5
	public static const BOTTOM_RIGHT:int = 6
	public static const BOTTOM_LEFT:int = 7

	private var _position:int
	private var _resizeCropMask:Function
	
	public function init():void {
		addEventListener(MouseEvent.MOUSE_DOWN, downHandler)
		draw()
	}
	
	private function downHandler(mouseEvent:MouseEvent):void {
		stage.addEventListener(MouseEvent.MOUSE_MOVE, moveHandler)
		stage.addEventListener(MouseEvent.MOUSE_UP, endHandler)
		stage.addEventListener(Event.MOUSE_LEAVE, endHandler)
	}
	
	private function moveHandler(mouseEvent:MouseEvent):void {
		_resizeCropMask(mouseEvent, _position)
	}
	
	private function endHandler(event:Event):void {
		stage.removeEventListener(MouseEvent.MOUSE_MOVE, moveHandler)
		stage.removeEventListener(MouseEvent.MOUSE_UP, endHandler)
		stage.removeEventListener(Event.MOUSE_LEAVE, endHandler)
	}
	
	public function draw(): void 
	{
		graphics.clear()
		graphics.lineStyle(LINE_THICKNESS, LINE_COLOR)
		graphics.beginFill(BACKGROUND_COLOR)
		
		const sizeGolden: Number = CONTROL_SIZE/2.4

		switch (_position) {
			case LEFT:
				graphics.drawRect( 0, -CONTROL_HALF_SIZE, sizeGolden, CONTROL_SIZE )
				graphics.lineStyle()							// invisible square for larger mouse target
				graphics.beginFill(0,0)
				graphics.drawRect( 0, -CONTROL_HALF_SIZE, CONTROL_SIZE, CONTROL_SIZE )
				break;
				
			case TOP:
				graphics.drawRect( -CONTROL_HALF_SIZE, 0, CONTROL_SIZE, sizeGolden )
				graphics.lineStyle()							// invisible square for larger mouse target
				graphics.beginFill(0,0)
				graphics.drawRect( -CONTROL_HALF_SIZE, 0, CONTROL_SIZE, CONTROL_SIZE )				
				break;
				
			case RIGHT:
				graphics.drawRect( -sizeGolden, -CONTROL_HALF_SIZE, sizeGolden, CONTROL_SIZE )
				graphics.lineStyle()							// invisible square for larger mouse target
				graphics.beginFill(0,0)
				graphics.drawRect( -CONTROL_SIZE, -CONTROL_HALF_SIZE, CONTROL_SIZE, CONTROL_SIZE )				
				break;
				
			case BOTTOM:
				graphics.drawRect( -CONTROL_HALF_SIZE, -sizeGolden, CONTROL_SIZE, sizeGolden )
				graphics.lineStyle()							// invisible square for larger mouse target
				graphics.beginFill(0,0)
				graphics.drawRect( -CONTROL_HALF_SIZE, -CONTROL_SIZE, CONTROL_SIZE, CONTROL_SIZE )						
				break;
				
			case TOP_LEFT:
				graphics.drawRect( 0, 0, CONTROL_SIZE, sizeGolden )			// horizontal bar with border
				graphics.beginFill( BACKGROUND_COLOR )			// vertical bar with border
				graphics.drawRect( 0, 0, sizeGolden, CONTROL_SIZE )
				graphics.beginFill( BACKGROUND_COLOR )			// overpaint the border
				graphics.lineStyle()
				graphics.drawRect( 1, 1, CONTROL_SIZE-LINE_THICKNESS, sizeGolden-LINE_THICKNESS )
				graphics.beginFill(0,0) 						// invisible square for larger mouse target
				graphics.drawRect( 0, 0, CONTROL_SIZE, CONTROL_SIZE )
				break;
			
			case TOP_RIGHT:
				graphics.drawRect( -CONTROL_SIZE, 0, CONTROL_SIZE, sizeGolden )  	// horizontal bar with border
				graphics.beginFill( BACKGROUND_COLOR )			// vertical bar with border
				graphics.drawRect( -sizeGolden, 0, sizeGolden, CONTROL_SIZE )
				graphics.beginFill( BACKGROUND_COLOR )			// overpaint the border
				graphics.lineStyle()
				graphics.drawRect( -CONTROL_SIZE+1, 1, CONTROL_SIZE-LINE_THICKNESS, sizeGolden-LINE_THICKNESS )
				graphics.beginFill(0,0)							// invisible square for larger mouse target
				graphics.drawRect( -CONTROL_SIZE, 0, CONTROL_SIZE, CONTROL_SIZE )
				break;
				
			case BOTTOM_RIGHT:
				graphics.drawRect( -CONTROL_SIZE, -sizeGolden, CONTROL_SIZE, sizeGolden )	// horizontal bar with border
				graphics.beginFill( BACKGROUND_COLOR )			// vertical bar with border
				graphics.drawRect( -sizeGolden, -CONTROL_SIZE, sizeGolden, CONTROL_SIZE )
				graphics.beginFill( BACKGROUND_COLOR ) 			// overpaint the border
				graphics.lineStyle()
				graphics.drawRect( -CONTROL_SIZE+1, -sizeGolden+1, CONTROL_SIZE-LINE_THICKNESS, sizeGolden-LINE_THICKNESS )
				graphics.beginFill(0,0) 						// invisible square for larger mouse target
				graphics.drawRect( -CONTROL_SIZE, -CONTROL_SIZE, CONTROL_SIZE, CONTROL_SIZE )
				break;
				
			case BOTTOM_LEFT:
				graphics.drawRect( 0, -sizeGolden, CONTROL_SIZE, sizeGolden )		// horizontal bar with border
				graphics.beginFill( BACKGROUND_COLOR )			// vertical bar with border
				graphics.drawRect( 0, -CONTROL_SIZE, sizeGolden, CONTROL_SIZE )
				graphics.beginFill( BACKGROUND_COLOR ) 			// overpaint the border
				graphics.lineStyle()
				graphics.drawRect( 1, -sizeGolden+1, CONTROL_SIZE-LINE_THICKNESS, sizeGolden-LINE_THICKNESS )
				graphics.beginFill(0,0) 						// invisible square for larger mouse target
				graphics.drawRect( 0, -CONTROL_SIZE, CONTROL_SIZE, CONTROL_SIZE )
				break;
		}
		graphics.endFill()
	}
	
	private function move():void {
	
	}
	
	public function dispose():void {
		removeEventListener(MouseEvent.MOUSE_DOWN, downHandler)
		_resizeCropMask = null
	}
}
}